<?php

$folder = "/tmp/rep.txt";
$folder1 = "/tmp/rep1.txt";
$save_file = "/tmp/save_rep.txt";
$log = "/tmp/log.json";

$var = array();
$params = array();

$y = 0;
$z = 0;
$k = 0;
$nb_lines_in_current = 0;

$file_folder = file($folder);
$nb_lines_folder = count($file_folder);

echo "Nombre de lignes dans ". $folder. " : ". $nb_lines_folder."\n";

$dir = fopen($folder, "r");
while(!feof($dir))
{
	$fich1 = fgets($dir);
	$fich1 = trim($fich1);
	if(strlen($fich1) != 0)
	{
		$chemin = split(" ", $fich1);
		$fich1 = $chemin[1];

		if(strlen($fich1) != 0)
		{
 			$contenu_fichier = file($fich1);
			$nb_ligne = count($contenu_fichier);
			echo "Nombre de lignes dans ". $fich1. " : ". $nb_ligne."\n";
			$nb_lines_in_current = $nb_lines_in_current + $nb_ligne;
		}
	}
}
echo "Total Nb de lignes de tous les fichiers: ". $nb_lines_in_current."\n";
fseek($dir, 0);
fclose($dir);

if (!file_exists($save_file)) 
{
	$file = fopen($save_file, "w");
	fclose($file);
}

$previous = fopen($save_file,"r+");
$nb_lines_in_previous = fgets($previous);

if (empty($nb_lines_in_previous))
	$nb_lines_in_previous = 0;

echo "previous: ".$nb_lines_in_previous."\n";

if ($nb_lines_in_current != $nb_lines_in_previous)
{	
	fseek($previous, 0);
	fputs($previous, $nb_lines_in_current);

	$dir = fopen($folder, "r");
	
	while(!feof($dir))
	{
		$fich1 = fgets($dir);
		$fich1 = trim($fich1);
		if(strlen($fich1) != 0)
		{
			$chemin = split(" ", $fich1);
			$fich1 = $chemin[1];

			if(strlen($fich1) != 0)
			{
	 			$contenu_fichier = file($fich1);
				$nb_ligne = count($contenu_fichier);
				echo "Nombre de lignes dans ". $fich1. " : ". $nb_ligne."\n";
				if ($nb_ligne > 0)
				{
					echo "Je suis rentré dans le fichier ".$fich1."\n";

					$rep = fopen($folder1, "r");
					$contenu = file($folder1);
					$nb = count($contenu);
					while($y < $nb)
					{
						$fich = fgets($rep);
						$fic = trim($fich);
						$regex = split(" ", $fic); 
						if(strlen($regex[1]) != 0)
						{
							$fichier = fopen($regex[1], "r");
							$contenu_fich = file($regex[1]);
							$nb_ligne_fich = count($contenu_fich);
							
							while($z < $nb_ligne_fich)
							{
								$lig = fgets($fichier);
								$ligne = trim($lig);
								
								if(strlen($ligne) != 0)
								{		
									$tab = split($regex[0], $ligne);
									for ($i=2;$i<count($regex);$i++)
									{	
										if (($k==0))
										{
											$k++;
											if (empty($tab[$regex[$i]]))
											{
												$k = 0;
											}	
											else
											{			
												$tab_date = split(" ",$tab[$regex[$i]]);	
												$params["date"] = $tab_date[0];	
												if (empty($tab_date[1]))
													$params["heure"] = $tab[$regex[$i]+1];
												else
													$params["heure"] = $tab_date[1];
											}
										}
										else	
										{	
											if($i==3)
												$params["level"] = $tab[$regex[$i]];	
												
											else if( ($i==4) && ($tab[$regex[$i]]!="") )
											 	$params["appli"] = $tab[$regex[$i]];
			                                  else if( ($i==4) && ($tab[$regex[$i]]=="") )
			                                     {
		                                          $pic = split("/", $params["level"]);
		                                          $trefle = split("_", $pic[1]);
		                                          $params["appli"] = $trefle[0];
			                                     }
											else
												{ 
													 if($tab[$regex[$i]]!="")
											 		    $params["message"] = $tab[$regex[$i]];
														 
													 else
                                           {
                                             $as = split("\((.*)\)", $params["level"]);
			                                  	$params["message"] = $as[1];
                                           }
												}		
										}
									}
									$k=0;				
								}
		 						array_push($var, $params);
								$z++;
							}	
							fclose($fichier);
							$l++;
							$z = 0;
						}
						$y++; 
					}
					$contenu = utf8_encode(json_encode(array("logs" => $var)));
					$logs = fopen($log, "w");
					fputs($logs, $contenu);
					fclose($logs);
					fclose($rep);
				}
					
				}
			}
	 	}
	fseek($dir, 0);
	fclose($dir);
}		
fclose($previous);
?>
